home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Dr. Windows 3
/
dr win3.zip
/
dr win3
/
PROGRAMR
/
CENVIW2.ZIP
/
CMM_VS_C.DOC
< prev
next >
Wrap
Text File
|
1993-09-13
|
21KB
|
488 lines
CEnvi Shareware Manual, Chapter 3:
Cmm versus C, for C Programmers
CEnvi unregistered version 1.003
13 September 1993
CEnvi Shareware User's Manual
Copyright 1993, Nombas, All Rights Reserved.
Published by Nombas, P.O. Box 875, Medford, MA 02155 USA
(617)391-6595
Thank you for trying this shareware version of CEnvi from Nombas.
3. Cmm versus C: The Cmm language for C programmers
This chapter is for those who already know how to program in the
C language. This chapter describes only those elements of Cmm that differ from standard C, and so if you don't already
understand C, then this shouldn't have much meaning for you.
Non-C programmers should instead look at the previous chapter.
Since it is assumed that readers of this chapter are already
knowledgeable in C, only those aspects of Cmm that differ from C
are described here. If it's not mentioned here, then assume that
Cmm behavior will be standard C.
Deviations from C are a result of these two harmonious Cmm
directives: Convenience and Safety. Cmm is different from C
where the change makes Cmm more convenient for small programs,
command-line code, or scripting files, or if unaltered C rules
encourage coding that is potentially unsafe.
3.1 . C Minus Minus
Cmm is "C minus minus" where the minuses are Type Declarations
and Pointers. If you already know C and can remember to forget
these two aspects of C (I repeat, no Type Declarations and no
Pointers) then you know Cmm. If you were to take C code, and
delete all the lines, code-words, and symbols that either declare
data types or explicitly point to data, then you would be left
with Cmm code; and although you would be removing bytes of source
code, you would not be removing capabilities.
All of the details below that compare Cmm against C follow from
the general rule:
*Cmm is C minus Type Declarations and minus Pointers.
3.2 . Data Types
The only recognized data types are Float, Long, and Byte. The
words "Float", "Long", and "Byte" do not appear in Cmm source
code; instead, the data types are determined by context. For
instance 6 is a Long, 6.6 is a Float, and '6' is a Byte. Byte is
unsigned, and the other types are signed.
3.3 . Automatic Type Declaration
There are no type declarators and no type casting. Types are
determined from context. If the code says "i=6" then i is a
Long, unless a previous statement has indicated otherwise.
For instance, this C code:
int Max(int a,int b)
{
int result;
result = ( a < b ) ? b : a ;
return result;
}
could become this Cmm code:
Max(a,b)
{
result = ( a < b ) ? b : a ;
return result;
}
3.4 . Array Representation
Arrays are used in Cmm much like they are in C, except that they
are stored differently: a first-order array (e.g., a string) is
stored in consecutive bytes in memory, but arrays of arrays are
not in consecutive memory locations. The C declaration "char
c[3][3];" would state that there are sixteen consecutive bytes in
memory. In Cmm a similar statement such as "c[2][2] = 'A'" would
tell you that there are (at least) three arrays of characters,
and the third array of arrays has (at least) three characters in
it; and although the characters in c[0] are in consecutive bytes,
and the characters in c[1] are in consecutive bytes, the two
arrays c[0] and c[1] are not necessarily adjacent in memory.
3.4 .1 Array Arithmetic
When one array is assigned to the other, as in:
foo = "cat";
goo = foo;
then both variables define the same array and start at the same
offset 0. In this case, if foo[2] is changed then you will find
that goo[2] has also been changed.
Integer addition and subtraction can also be performed on arrays.
Array addition or subtraction sets where the array is based. By
altering the previous code segment to:
foo = "cat";
goo = foo + 1;
goo and foo would now be arrays containing the same data, except
that now goo is based one element further, and foo[2] is now the
same data as goo[1].
To demonstrate:
foo = "cat"; // foo[0] is 'c', foo[1] = 'a'
goo = foo + 1;// goo[0] is 'a', goo[-1] = 'c'
goo[0] = 'u'; // goo[0] is 'u', foo[1] = 'u', foo is "cut"
goo++; // goo[0] is 't', goo[-2] = 'c'
goo[-2] = 'b' // goo[0] is 't', foo[0] = 'b', foo is "but"
3.4 .2 Automatic Array Allocation
Arrays are dynamic, and any index, (positive or negative) into an
array is always valid. If an element of an array is referred to,
then the Cmm must see to it that such an element will exist. For
instance if the first statement in the Cmm source code is "foo[4]
= 7;" then the Cmm interpreter will make an array of 5 integers
referred to by the variable foo. If a statement further on
refers to "foo[6]" then the Cmm interpreter will grow foo, if it
has to, to ensure that the element foo[6] exists. This works
with negative indices as well. When you refer to foo[-10], then
foo is grown in the other direction if it needs to be, but foo[4]
will still refer to that "7" you put there earlier. Arrays can
reach any dimension order, so that foo[6][7][34][-1][4] is a
valid value.
3.5 . Structures
Structures are created dynamically, and their elements are not
necessarily contiguous in memory. When CEnvi comes across the
statement 'foo.animal = "dog"' it creates a structure element of
foo that is referred to as "animal" and is an array of
characters, and this "animal" variable is thereafter carried
around with "foo" (much like a stem variable in REXX). The
resulting code looks very much like regular C code, except that
there is not a separate structure definition anywhere.
This C code:
struct Point {
int Row;
int Column;
};
struct Square {
struct Point BottomLeft;
struct Point TopRight;
};
void main()
{
struct Square sq;
int Area;
sq.BottomLeft.Row = 1;
sq.BottomLeft.Column = 15;
sq.TopRight.Row = 82;
sq.TopRight.Column = 120;
Area = AreaOfASquare(sq);
}
int AreaOfASquare(struct Square s)
{
int width, height;
width = s.TopRight.Column - s.BottomLeft.Column + 1;
height = s.TopRight.Row - s.BottomLeft.Row + 1;
return( length * height );
}
can be changed into the equivalent Cmm code simply be removing
declaration lines, resulting in:
main()
{
sq.BottomLeft.Row = 1;
sq.BottomLeft.Column = 15;
sq.TopRight.Row = 82;
sq.TopRight.Column = 120;
Area = AreaOfASquare(sq);